﻿

<!DOCTYPE HTML>
<html>
<head> 
<title>Vocabulary Test</title> 
<script language="javascript"> 

var xmlhttp=false;

var ds= new Array();
ds[0] = "I am also";
var word= new Array();
word[0] = "hungery";
var randomnumber = new Array();
var count = 0;
var flag = 0;
var textname = new Array();
textname[0] = "textname";
var tname;


if (!xmlhttp && typeof XMLHttpRequest!='undefined') { 
	try {
      xmlhttp = new XMLHttpRequest();
    } catch (e) {
      xmlhttp = false;
    }
}

// this loadDoc uses proxy implmentation
// please run this application with PHP5+
function loadDocWithProxy() {
	count = count + 1;
	var proxy = "./simpleproxy2.php";
	word[count] = document.forms[0].textArea1.value;
	document.form1.textArea2.value = document.forms[0].textArea2.value + ' ' + word[count];
	document.form1.textArea1.value = '';
	
	if (xmlhttp)
	{
		xmlhttp.open("GET", proxy + '?q='+word[count]+ '&type=synonyms', true);
		xmlhttp.onreadystatechange=function() {
			if (xmlhttp.readyState==4)
			{
				if (xmlhttp.status==200)
				{					
					ds[count]=xmlhttp.responseText;					
				}
			}
		}
		xmlhttp.send(null);
	} else {
		alert("Cannot create a XHR instance");
	}
}

Array.prototype.contains = function(obj) {
    var t = this.length;
    while (t--) {
        if (this[t] == obj) {
            return true;
        }
    }
    return false;
}

function startTest() {
	
	document.write(document.forms[0].textArea2.value);
	document.write("<table border='0'>");
	for (i=0;i<count;i++)
    { var temprand=Math.floor(Math.random()*count + 1);

	  while (randomnumber.contains(temprand)) {
		
			temprand=Math.floor(Math.random()*count + 1);
		}
	
	  randomnumber[i] = temprand;
	  document.write("<tr><td>");
	  document.write(i+1);
	  document.write(". ");
	  document.write(ds[randomnumber[i]]);
	  document.write("</td><td>");
	  document.write('<textarea cols=10 row=1> </textarea>');
	  document.write("</td></tr>");
    }
    document.write("</table>");
	document.write('<button onclick="showAnswers();">Answers</button>');	
}

function showAnswers() {
	document.write("<br>Answers:</br>");
	for (k=0;k<count;k++) {
		document.write(k+1+". ");
		document.write(word[randomnumber[k]] + "  ");
	} 
}

</script> 
<style> 
body, td {
	font-family: Arial;
	font-size: 13px;
	}
</style> 
</head> 
 
<body> 
<h1>Vocabulary-Test</h1> 
Enter a word:
<form name=form1> 
<textarea cols=30 rows=2 name=textArea1></textarea>
<input type=button value="Enter" onClick="loadDocWithProxy();">
<p><textarea cols = 40 rows = 5 name=textArea2> </textarea></p>
<input type=button value="OK" onClick="startTest();">
</form> 


 
</body> 
</html> 